package org.calzz.rpc.envelope;

import org.calzz.jajb.annotation.Converter;

/**
 * When using HTTP POST, the call is expressed in the HTTP request body as a
 * JSON Object that carries the following members.
 * 
 * @author Marek Brodziak <marek.brodziak@gmail.com>
 * @author Piotr Pejas <piotr.pejas@gmail.com>
 * @since Mar 6, 2008
 */
public class JsonRequest {

	private String version;
	private String method;
	private Object params;
	private Object id;

	/**
	 * @return REQUIRED. A String containing the name of the procedure to be
	 *         invoked. Procedure names that begin with the word system followed
	 *         by a period character (U+002E or ASCII 46) are reserved. In other
	 *         words, a procedure named system.foobar is considered to have
	 *         reserved semantics.
	 */
	public String getMethod() {
		return method;
	}

	/**
	 * @param method REQUIRED. A String containing the name of the procedure to
	 *            be invoked. Procedure names that begin with the word system
	 *            followed by a period character (U+002E or ASCII 46) are
	 *            reserved. In other words, a procedure named system.foobar is
	 *            considered to have reserved semantics.
	 */
	public void setMethod(String method) {
		this.method = method;
	}

	/**
	 * @return OPTIONAL. An Array or Object that holds the actual parameter
	 *         values for the invocation of the procedure.
	 */
	@Converter(DummyConverter.class)
	public Object getParams() {
		return params;
	}

	/**
	 * @param params OPTIONAL. An Array or Object that holds the actual
	 *            parameter values for the invocation of the procedure.
	 */
	public void setParams(Object params) {
		this.params = params;
	}

	/**
	 * @return id OPTIOINAL. A request identifier that can be of any JSON type.
	 *         This member is essentially maintained for backward compatibility
	 *         with version 1.0 of the specification where it was used to
	 *         correlate a response with its request. If this member is present
	 *         then the server MUST repeat it verbatim on the response.
	 */
	@Converter(DummyConverter.class)
	public Object getId() {
		return id;
	}

	/**
	 * @param id OPTIOINAL. A request identifier that can be of any JSON type.
	 *            This member is essentially maintained for backward
	 *            compatibility with version 1.0 of the specification where it
	 *            was used to correlate a response with its request. If this
	 *            member is present then the server MUST repeat it verbatim on
	 *            the response.
	 */
	public void setId(Object id) {
		this.id = id;
	}

	/**
	 * @return name of the controller to be used extracted from the call
	 * @throws JsonInvocationException
	 */
	public String getControllerName() throws JsonInvocationException {
		try {
			return method.substring(0, method.lastIndexOf('.'));
		} catch (Exception e) {
			throw new JsonInvocationException(JsonInvocationError.BAD_CALL, "Malformed method name", e);
		}
	}

	/**
	 * @return name of the method to be invoked extracted from the call
	 */
	public String getMethodName() throws JsonInvocationException {
		try {
			return method.substring(method.lastIndexOf('.') + 1);
		} catch (Exception e) {
			throw new JsonInvocationException(JsonInvocationError.BAD_CALL, "Malformed method name", e);
		}
	}

	/**
	 * @return REQUIRED. A String specifying the version of the JSON-RPC
	 *         protocol to which the client conforms. An implementation
	 *         conforming to this specification MUST use the exact String value
	 *         of "1.1" for this member.
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * Used for setting JSON-RPC version manually while processing GET requests.
	 * @param REQUIRED. A String specifying the version of the JSON-RPC protocol
	 *            to which the client conforms. An implementation conforming to
	 *            this specification MUST use the exact String value of "1.1"
	 *            for this member.
	 */
	public void setVersion(String version) {
		this.version = version;
	}

}
